package com.ddz.library.permission

import android.Manifest
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.PowerManager
import android.provider.Settings
import android.provider.Settings.SettingNotFoundException
import android.text.TextUtils
import androidx.annotation.RequiresApi
import androidx.annotation.RequiresPermission
import androidx.fragment.app.FragmentActivity
import com.ddz.library.JKLibrary

object JKPermissionUtils {
    /**
     * 是否有在最上层运行的权限
     */
    @RequiresApi(Build.VERSION_CODES.M)
    @JvmStatic
    fun canDrawOverlays(): Boolean {
        return Settings.canDrawOverlays(JKLibrary.context)
    }

    /**
     * 获取最上层应用运行权限
     */
    @RequiresPermission(Manifest.permission.SYSTEM_ALERT_WINDOW)
    @RequiresApi(Build.VERSION_CODES.M)
    fun requestDrawOverlays(activity: FragmentActivity, requestCode: Int) {
        val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
        intent.data = Uri.parse("package:${activity.packageName}")
        activity.startActivityForResult(intent, requestCode)
    }

    /**
     * 是否开启无障碍
     */
    @Throws(SettingNotFoundException::class)
    fun isEnableAccessibility(accessibilityClass: Class<*>): Boolean {
        JKLibrary.context?.contentResolver.let { contentResolver ->
            val ok =
                Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED)
            if (ok == 1) {
                val sss = TextUtils.SimpleStringSplitter(':')
                val settingsValue = Settings.Secure.getString(
                    contentResolver,
                    Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
                ) ?: return false
                sss.setString(settingsValue)
                while (sss.hasNext()) {
                    val serviceName = sss.next()
                    if (serviceName.contains(accessibilityClass.simpleName)) {
                        return true
                    }
                }
            }
        }
        return false
    }

    /**
     * 获取无障碍服务权限
     */
    fun requestAccessibility(activity: FragmentActivity, requestCode: Int) {
        val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        activity.startActivityForResult(intent, requestCode)
    }

    /**
     * 是否加入了应用运行白名单
     */
    @RequiresApi(Build.VERSION_CODES.M)
    fun isIgnoringBatteryOptimizations(): Boolean {
        val powerManager = JKLibrary.context!!.getSystemService(Context.POWER_SERVICE)
            ?: return false
        return (powerManager as PowerManager).isIgnoringBatteryOptimizations(JKLibrary.context!!.packageName)
    }

    /**
     * 申请加入应用运行白名单
     */
    @RequiresPermission(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
    @RequiresApi(Build.VERSION_CODES.M)
    fun requestIgnoringBatteryOptimizations(fragmentActivity: FragmentActivity, requestCode: Int) {
        try {
            val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
            intent.data = Uri.parse("package:${JKLibrary.context!!.packageName}")
            fragmentActivity.startActivityForResult(intent, requestCode)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}